package com.kfh.ybracelet.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kfh.ybracelet.R;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.base.HomeActivity;
import com.kfh.ybracelet.net.HttpUtil;
import com.kfh.ybracelet.utils.CommonUtils;
import com.kfh.ybracelet.utils.Constant;

import de.greenrobot.event.EventBus;


/**
 * 输入宝宝邀请码页面
 * Created by Administrator on 2015/8/13.
 */
public class EntryCodeActivity extends BaseActivity {
    private EditText et_entrybaby_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_entry_code);
        init();

    }

    private void getData() {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("inviteKey", et_entrybaby_code.getText().toString().trim());

        HttpUtil.setPostRequest("刷新中...", this, Constant.WEB_ROOT + Constant.CHECKINVITEKEY,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {

                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

//                            org.json.JSONObject result = new org.json.JSONObject(str);

//                            if (result.getInt("code") == Constant.STATUS_SUCCESS) {
                                showToast("绑定成功!");

                                EventBus.getDefault().post(true);//通知Locationfragment页面重新定位孩子位置

                                Intent mIntent = new Intent(EntryCodeActivity.this, HomeActivity.class);
                                startActivity(mIntent);
                                finish();

//                            } else {
//                                showToast(Constant.showErrorMsg(result.getInt("code"), EntryCodeActivity.this));
//                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast(Constant.NODATA);
                        }
                    }
                }, null);

    }

    private void init() {
        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(getResources().getString(R.string.entry_code));
        TextView tv_done = (TextView) findViewById(R.id.tv_done);
        et_entrybaby_code = (EditText) findViewById(R.id.et_entrybaby_code);

        tv_done.setOnClickListener(this);
        iv_left.setOnClickListener(this);

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left:
                finish();
                break;
            case R.id.tv_done:
                String name = et_entrybaby_code.getText().toString().trim();

                if (CommonUtils.isNull(name)) {

                    showToast("请输入邀请码");
                    return;
                }

                getData();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mApplication.cancelPendingRequests(getClass().getName());
    }
}
